﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using csla4skeleton.DataAcess;
using Csla.Data;

namespace Accounting.Library
{
    public class KhoService
    {
        /// <returns>So luong hang ton</returns>
        /// <summary>
        /// Lay so luong ton kho cua 1 hang hoa trong 1 kho cu the
        /// </summary>
        /// <param name="ctx">Data access context</param>
        /// <param name="KhoId">Id kho</param>
        /// <param name="HangHoaId">Id hang hoa</param>
        /// <param name="maKho">out: Ma kho</param>
        /// <param name="maHangHoa">out: Ma hang hoa</param>
        /// <returns></returns>
        public static double LaySoLuongTon(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int KhoId, int HangHoaId, out string maKho, out string maHangHoa)
        {
            try
            {
                Accounting.DataAccess.DM_Kho_HangHoa kho_hanghoa = (from p in ctx.DataContext.DM_Kho_HangHoas
                                                                    where (p.KhoId == KhoId && p.HangHoaId == HangHoaId)
                                                                    select p).Single();
                maKho = kho_hanghoa.DM_Kho.Ma;
                maHangHoa = kho_hanghoa.DM_HangHoa.Ma;

                double soLuongTon = kho_hanghoa.TonDauKy + kho_hanghoa.PhatSinhTrongThang;
                return soLuongTon;
            }
            catch
            {
                maKho = (from p in ctx.DataContext.DM_Khos
                         where (p.Khoa == KhoId)
                         select p.Ma).Single();

                maHangHoa = (from p in ctx.DataContext.DM_HangHoas
                         where (p.Khoa == HangHoaId)
                         select p.Ma).Single();

                return 0;
            }
        }

        /// <summary>
        /// Xuat kho hang hoa
        /// </summary>
        /// <param name="ctx">Data access context</param>
        /// <param name="KhoId">Ma kho</param>
        /// <param name="HangHoaId">Ma Hang Hoa</param>
        /// <param name="SoLuong">So luong xuat</param>
        /// <returns>Ket qua cua giao dich</returns>
        public static bool XuatKho(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int KhoId, int HangHoaId, double SoLuong)
        {
            try
            {
                Accounting.DataAccess.DM_Kho_HangHoa kho_hanghoa = (from p in ctx.DataContext.DM_Kho_HangHoas
                                                                    where (p.KhoId == KhoId && p.HangHoaId == HangHoaId)
                                                                    select p).Single();

                double soLuongTon = kho_hanghoa.TonDauKy + kho_hanghoa.PhatSinhTrongThang;
                if (soLuongTon < SoLuong)
                {
                    throw new LuongTonKhongDuException();
                }

                kho_hanghoa.PhatSinhTrongThang -= SoLuong;
                ctx.DataContext.SubmitChanges();

                return true;
            }
            catch
            {
                throw new KhongTimThayHangException();
            }
        }

        /// <summary>
        /// Cap nhat so luong xuat kho, dung trong truong hop cap nhat phieu xuat kho
        /// </summary>
        /// <param name="ctx">Data access context</param>
        /// <param name="KhoId">Ma kho</param>
        /// <param name="HangHoaId">Ma hang hoa</param>
        /// <param name="SoLuongXuatTruoc">So luong dang luu trong database</param>
        /// <param name="SoLuongXuatCapNhat">So luong moi se duoc cap nhat</param>
        /// <returns>Ket qua cua giao dich</returns>
        public static bool CapNhatSoLuongXuatKho(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int KhoId, int HangHoaId, double SoLuongXuatTruoc, double SoLuongXuatCapNhat)
        {
            try
            {
                Accounting.DataAccess.DM_Kho_HangHoa kho_hanghoa = (from p in ctx.DataContext.DM_Kho_HangHoas
                                                                    where (p.KhoId == KhoId && p.HangHoaId == HangHoaId)
                                                                    select p).Single();

                double phatSinhTrongThang = kho_hanghoa.PhatSinhTrongThang + SoLuongXuatTruoc;
                double soLuongTon = kho_hanghoa.TonDauKy + phatSinhTrongThang;
                if (soLuongTon < SoLuongXuatCapNhat)
                {
                    throw new LuongTonKhongDuException();
                }

                kho_hanghoa.PhatSinhTrongThang = phatSinhTrongThang - SoLuongXuatCapNhat;
                ctx.DataContext.SubmitChanges();

                return true;
            }
            catch
            {
                throw new KhongTimThayHangException();
            }
        }

        /// <summary>
        /// Xoa so luong xuat kho, dung trong truong hop xoa phieu xuat kho.
        /// </summary>
        /// <param name="ctx">Data access context</param>
        /// <param name="KhoId">Ma kho</param>
        /// <param name="HangHoaId">Ma hang hoa</param>
        /// <param name="SoLuong">So luong xuat kho truoc day</param>
        /// <returns>Ket qua cua giao dich</returns>
        public static bool XoaSoLuongXuatKho(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int KhoId, int HangHoaId, double SoLuong)
        {
            try
            {
                Accounting.DataAccess.DM_Kho_HangHoa kho_hanghoa = (from p in ctx.DataContext.DM_Kho_HangHoas
                                                                    where (p.KhoId == KhoId && p.HangHoaId == HangHoaId)
                                                                    select p).Single();

                kho_hanghoa.PhatSinhTrongThang += SoLuong;
                ctx.DataContext.SubmitChanges();

                return true;
            }
            catch
            {
                throw new KhongTimThayHangException();
            }
        }

        /// <summary>
        /// Nhap kho hang hoa
        /// </summary>
        /// <param name="ctx">Dataccess context</param>
        /// <param name="KhoId">Ma kho</param>
        /// <param name="HangHoaId">Ma hang hoa</param>
        /// <param name="SoLuong">So luong nhap kho</param>
        /// <returns>Ket qua cua giao dich</returns>
        public static bool NhapKho(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int KhoId, int HangHoaId, double SoLuong)
        {
            try
            {
                Accounting.DataAccess.DM_Kho_HangHoa kho_hanghoa = (from p in ctx.DataContext.DM_Kho_HangHoas
                                                                    where (p.KhoId == KhoId && p.HangHoaId == HangHoaId)
                                                                    select p).Single();

                kho_hanghoa.PhatSinhTrongThang += SoLuong;
                ctx.DataContext.SubmitChanges();

                return true;
            }
            catch (InvalidOperationException)
            {
                Accounting.DataAccess.DM_Kho_HangHoa entity = new Accounting.DataAccess.DM_Kho_HangHoa();
                entity.KhoId = KhoId;
                entity.HangHoaId = HangHoaId;
                entity.TonDauKy = 0;
                entity.PhatSinhTrongThang = SoLuong;

                ctx.DataContext.DM_Kho_HangHoas.InsertOnSubmit(entity);
                ctx.DataContext.SubmitChanges();

                return true;
            }
            catch (ArgumentNullException)
            {
                return false;
            }
        }

        /// <summary>
        /// Cap nhat so luong nhap kho, dung trong truong hop cap nhat phieu nhap kho
        /// </summary>
        /// <param name="ctx">Data access context</param>
        /// <param name="KhoId">Ma kho</param>
        /// <param name="HangHoaId">Ma hang hoa</param>
        /// <param name="SoLuongNhapTruoc">So luong nhap duoc luu trong database</param>
        /// <param name="SoLuongNhapCapNhat">So luong nhap se dc cap nhat</param>
        /// <returns>Ket qua cua giao dich</returns>
        public static bool CapNhatSoLuongNhapKho(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int KhoId, int HangHoaId, double SoLuongNhapTruoc, double SoLuongNhapCapNhat)
        {
            try
            {
                Accounting.DataAccess.DM_Kho_HangHoa kho_hanghoa = (from p in ctx.DataContext.DM_Kho_HangHoas
                                                                    where (p.KhoId == KhoId && p.HangHoaId == HangHoaId)
                                                                    select p).Single();

                double phatSinhTrongThang = kho_hanghoa.PhatSinhTrongThang - SoLuongNhapTruoc;
                kho_hanghoa.PhatSinhTrongThang = phatSinhTrongThang + SoLuongNhapCapNhat;
                ctx.DataContext.SubmitChanges();

                return true;
            }
            catch
            {
                throw new KhongTimThayHangException();
            }
        }

        /// <summary>
        /// Xoa so luong nhap kho, dung trong truong hop xoa phieu nhap kho.
        /// </summary>
        /// <param name="ctx">Data access context</param>
        /// <param name="KhoId">Ma kho</param>
        /// <param name="HangHoaId">Ma hang hoa</param>
        /// <param name="SoLuong">So luong nhap truoc day</param>
        /// <returns>Ket qua cua giao dich</returns>
        public static bool XoaSoLuongNhapKho(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int KhoId, int HangHoaId, double SoLuong)
        {
            try
            {
                Accounting.DataAccess.DM_Kho_HangHoa kho_hanghoa = (from p in ctx.DataContext.DM_Kho_HangHoas
                                                                    where (p.KhoId == KhoId && p.HangHoaId == HangHoaId)
                                                                    select p).Single();

                kho_hanghoa.PhatSinhTrongThang -= SoLuong;
                ctx.DataContext.SubmitChanges();

                return true;
            }
            catch
            {
                throw new KhongTimThayHangException();
            }
        }
    }
}
